package com.gaattc.controller.admin;

import com.gaattc.model.Result;
import com.gaattc.model.bo.ChangeOrderBO;
import com.gaattc.model.bo.OrdersByPageBO;
import com.gaattc.model.vo.OrderVO;
import com.gaattc.model.vo.OrdersByPageVO;
import com.gaattc.service.OrderService;
import com.gaattc.service.OrderServiceImpl;
import com.gaattc.utils.HttpUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/api/admin/order/*")
public class OrderServlet extends HttpServlet {

    private OrderService orderService = new OrderServiceImpl();

    private Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/order/", "");
        if("ordersByPage".equals(action)) {
            ordersByPage(request, response);
        } if("changeOrder".equals(action)) {
            changeOrder(request, response);
        }
    }

    /**
     * 更改订单
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void changeOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        ChangeOrderBO changeOrderBO = gson.fromJson(requestBody, ChangeOrderBO.class);
        orderService.changeOrder(changeOrderBO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 根据页面查询订单
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void ordersByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        OrdersByPageBO ordersByPageBO = null;
        try {
             ordersByPageBO = gson.fromJson(requestBody, OrdersByPageBO.class);
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println(gson.toJson(Result.error("参数不合法")));
            return;
        }
        Map<String, Object> result = orderService.ordersByPage(ordersByPageBO);
        response.getWriter().println(gson.toJson(Result.ok(result)));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/order/", "");
        if("deleteOrder".equals(action)) {
            deleteOrder(request, response);
        } else if("order".equals(action)) {
            order(request, response);
        }
    }

    /**
     * 获取订单信息
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void order(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = 0;
        try {
            id = Integer.parseInt(request.getParameter("id"));
        } catch (Exception e) {
            response.getWriter().println(gson.toJson(Result.error("请求参数不合法")));
            return;
        }
        OrderVO orderVO = orderService.order(id);
        response.getWriter().println(gson.toJson(Result.ok(orderVO)));
    }

    /**
     * 删除订单
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void deleteOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = 0;
        try {
            id = Integer.parseInt(request.getParameter("id"));
        } catch (Exception e) {
            response.getWriter().println(gson.toJson(Result.error("请求参数不合法")));
            return;
        }
        int code = orderService.deleteOrder(id);
        if(code == 0) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("删除失败")));
    }
}
